<template>
	<view class="route-gooddetail">
		<view class="lunbo">
			<u-swiper :list="list1" imgMode="" radius="0" bgColor="#ffffff" :autoplay=" false" indicator
				indicatorMode="dot" circular height="240"></u-swiper>
			<view class="content">
				<view class="card info-cont">
					<view class="name">
						{{goodsInfo.name}}
					</view>
					<view class="price">
						<view class="unit">
							￥
						</view>
						<view class="val">
							{{goodsInfo.price}}
						</view>
					</view>
					<!-- <view class="desc">
						<view class="kc">
							库存：0
						</view>
					</view> -->
				</view>

				<view class="card detail-cont">
					<view class="tit">
						商品详情
					</view>
				</view>
				<view class="card hx-cont">
					<view class="tit">
						价格说明
					</view>
					<view class="infoInner">
						<view class="item">
							<view class="itemtitle">
								<view style="color: #ff6600;">*</view>
								<view>划线价</view>
							</view>
							<view style="font-size: 24rpx;color: #999;padding:20rpx 0 ">
								划线价为参考价，并非原价或线下实体门店销售价格。该划线价可能是商品（含服务）的网络零售指导价，品牌供应商提供的正品零售价（如厂商指导价，建议零售价等）
								或该商品在本平台上曾经展示过的销售价格等，由于地区，时间的差异性和市场行情波动，零售指导价，销售价等可能会与您购物时展示的不一致，该价格仅供您参考。
							</view>
						</view>
						<view class="item">
							<view class="itemtitle">
								<view style="color: #ff6600;">*</view>
								<view>未划线价</view>
							</view>
							<view style="font-size: 24rpx;color: #999;padding:20rpx 0 ">
								未划线价是当前商品或服务在本平台的销售价格（不含包装价格），是您最终决定是否噶偶买商品的依据，此说明仅当出现价格比较时有效，若商家单独对划线价格进行说明的，以商家描述为准
							</view>
						</view>
						<view class="item">
							<view class="itemtitle">
								<view style="color: #ff6600;">*</view>
								<view>预估到手价</view>
							</view>
							<view style="font-size: 24rpx;color: #999;padding:20rpx 0 ">
								是商品价格基础上减去各种折扣，可用优惠政策（不包含红包）叠加金额之后的一种预估商品价格，不包含商品打包费和配送费。计算公式为：“预估到手价”=商品原价-会员折扣-新客活动-满减/直降活动-商家代金卷。用户在进入结算页面之后，根据满足条件可享受的活动，红包，代金卷等优惠组合，可能导致最终到手价与显示”预估到手价”有差异和变化，并且不同用户因为可以享受的优惠不同（造成这种不同的原因可能包括是否属于新客，领取的代金卷不同，代金卷消耗情况等）而导致最终到手价不同
							</view>
						</view>
						<view class="item">
							<view class="itemtitle">
								<view style="color: #ff6600;">*</view>
								<view>配送费描述</view>
							</view>
							<view style="font-size: 24rpx;color: #999;padding:20rpx 0 ">
								配送费受电梯房，楼层的因素影响，店内页面（如上架页及商品描述详情页）展示的配送费仅为预估配送金额，最终金额以及提交订单页显示的配送费金额为准。
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="footer">
				<view class="footer-left" @click="handleGoCar">
					<view class="imgcont">
						<image class="img" src="../../static/images/car.png" mode=""></image>
					</view>
					<view class="text">
						购物车
					</view>
				</view>
				<view class="footer-right">
					<view class="btn jia" @click="handleShowPop">
						加入购物车
					</view>
					<view class="btn mai" @click="handleShowPop">
						立即购买
					</view>
				</view>
			</view>

		</view>
		<u-popup :show="showPop" :round="10" closeable :closeOnClickOverlay="true" @close="handleClosePop">
			<view class="popCont">
				<view :scroll-y="true" :scroll-x="false" class="cont-box">
					<view class="top">
						<view class="img-cont">
							<image class="img" :src="goodsInfo.image" mode=""></image>
						</view>
						<view class="info">
							<view class="name">
								{{goodsInfo.name}}
							</view>
							<view class="price">
								<view class="unit">
									￥
								</view>
								<view class="val">
									{{activeInfo.price}}
								</view>
							</view>
						</view>
					</view>

					<view class="typeList">
						<view @click="handleCheckActive(item)"
							:class="`typeItem ${activeInfo.id===item.id?'active':''}`"
							v-for="(item,index) in goodsInfo.List">
							{{item.name}}
						</view>
					</view>

					<view class="middle">
						<view class="text">
							数量
						</view>
						<view class="num">
							<u-number-box button-size="26" integer :min="1" v-model="num"></u-number-box>
						</view>
					</view>
				</view>

				<view class="popFooter">
					<view class="btn jia" @click="handleAddCar">
						加入购物车
					</view>
					<view class="btn mai" @click="handleBuy">
						立即购买
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script setup>
	import handleCache from "@/utils/cache.js"
	import {
		goodsApi,
		userApi
	} from '@/api/index.js'
	import {
		ref,
		computed
	} from 'vue'
	import {
		onReady,
		onShow,
		onLoad,
		onShareAppMessage
	} from '@dcloudio/uni-app'

	const list1 = ref([])
	const showPop = ref(false)
	const num = ref(1)
	const goodsId = ref('')
	const goodsInfo = ref('')
	const activeInfo = ref('')
	const userInfo = ref('')
	const shareUserId = ref('')
	const shareShanghuId = ref('')
	const shareData = computed(() => { // 分享的数据
		return {
			title: `${uni.getStorageSync('userInfo').shanghuname||''}-${goodsInfo.value.name}`,
			path: `/pages/gooddetail/gooddetail?id=${goodsId.value}&pid=${uni.getStorageSync('userInfo')?.value?.id||''}&shanghuid=${uni.getStorageSync('userInfo')?.value?.shanghuid||''}`, // 默认为当前页面路径
			desc: '快来看看',
			imageUrl: goodsInfo.value.image
		}
	})


	onLoad(async (options) => {


		if (options.id) {
			goodsId.value = Number(options.id)
			await goodsApi.goodinfo({
				goodid: goodsId.value
			}, {
				loading: {
					text: '加载中'
				}
			}).then((res) => {
				goodsInfo.value = res.data
				list1.value = [res.data.image]
				if (goodsInfo.value.List && goodsInfo.value.List.length) {
					activeInfo.value = goodsInfo.value.List[0]
				} else {
					activeInfo.value = res.data
				}
			})
			uni.$mpShare = shareData.value
		}
		if (options.pid) {
			getApp().globalData.shareUserId = Number(options.pid)
			shareUserId.value = Number(options.pid)
		}
		if (options.shanghuid) {
			getApp().globalData.shareShanghuId = Number(options.shanghuid)
			shareShanghuId.value = Number(options.shanghuid)
		}
	})
	onShow(async () => {
		userInfo.value = handleCache.get('userInfo')
		if (userInfo.value) {
			if (shareShanghuId.value) {
				await userApi.userstore({
					uid: userInfo.value.id,
					shanghuid: shareShanghuId.value
				}).then((res) => {
					if (res.code == 0) {
						let data = {
							...userInfo.value
						}
						data.shanghuid = shareShanghuId.value
						userInfo.value = data
						handleCache.remove('userInfo')
						handleCache.set('userInfo', data, 'forever')
					}
					shareShanghuId.value = ''
				})
			}
			if (shareUserId.value && userInfo.value.id !== shareUserId.value) {
				let {
					data
				} = await userApi.bangdingsj({
					uid: userInfo.value.id,
					pid: shareUserId.value
				})
				shareUserId.value = ''
			}
		}
	})
	const handleCheckActive = (data) => {
		if (activeInfo.value.id === data.id) {
			return
		}
		num.value = 1
		activeInfo.value = data

	}
	const handleClosePop = () => {
		showPop.value = false
	}
	const handleShowPop = () => {
		showPop.value = true
	}
	const handleGoCar = () => {
		if (!userInfo.value) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
			return
		}
		uni.navigateTo({
			url: '/pages/car/car'
		})
	}
	const handleAddCar = async () => {
		if (!userInfo.value) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
			return
		}
		let req = {
			goodid: goodsId.value,
			uid: userInfo.value.id,
			num: num.value,
			guigeid: activeInfo.value.id,
			guige: activeInfo.value.name,
			shanghuid: userInfo.value.shanghuid
		}
		let {
			code,
			data
		} = await goodsApi.goodcartadd(req, {
			loading: {
				text: '请稍等'
			}
		})
		if (code === 0) {
			handleClosePop()
			uni.showToast({
				icon: 'none',
				title: '加入成功'
			})
		}


	}
	const handleBuy = async () => {
		if (!userInfo.value) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
			return
		}
		let req = {
			goodid: goodsId.value,
			uid: userInfo.value.id,
			num: num.value,
			guigeid: activeInfo.value.id,
			guige: activeInfo.value.name,
			shanghuid: userInfo.value.shanghuid
		}
		let {
			code,
			data
		} = await goodsApi.cartbuy(req, {
			loading: {
				text: '请稍等'
			}
		})

		if (code === 0) {
			uni.navigateTo({
				url: '/pages/playorder/playorder?carId=' + data
			})
		}
	}
</script>

<style lang="scss" scoped>
	@import 'index.scss'
</style>